/* $RCSFile: HtmlTabDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/HTMLTab/HtmlTabDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:55  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Collections;
using System.Globalization;

using AddonNice.Configuration;
using AddonNice.Diagnostics;


namespace AddonNice.Settings.PageModules.HtmlTab
{
	/// <summary>
	/// Class that encapsulates all data logic necessary to add/query/delete
	/// HtmlTabs within the Portal database.
	/// </summary>
    public class HtmlTabDB : DBDataStore
    {
        public DataTable GetTabs(int moduleID) 
        {
            SqlDataAdapter myAdapt			    =   new SqlDataAdapter("dbo.adn_GetHtmlTabs", myConnection);
            myAdapt.SelectCommand.CommandType   =   CommandType.StoredProcedure;

            AddModuleIDParameters(myAdapt.SelectCommand,moduleID);

			DataTable myDataSet					=   new DataTable();
			try
			{
				myAdapt.Fill(myDataSet);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabDB GetTabs ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
			return CheckDataTableForDates(myDataSet);
        }
		/// <summary>
		/// The GetSingleTab method returns a SqlDataReader containing details
		/// about a specific tab from the HtmlTabs database table.
		/// </summary>
        public SqlDataReader GetSingleTab(int itemID) 
        {
            SqlCommand myCommand			=   new SqlCommand("dbo.adn_GetSingleHtmlTab", myConnection);
            myCommand.CommandType			=   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);


			SqlDataReader result			=   null;
			try
			{
				myConnection.Open();
				result						=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabDB GetSingleTab itemID: {0}, ex: {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
        }

		/// <summary>
		/// The DeleteTab method deletes the specified tab from
		/// the HtmlTabs database table.
		/// </summary>
        public void DeleteTab(int itemID) 
        {
            SqlCommand myCommand			= new SqlCommand("dbo.adn_DeleteHtmlTab", myConnection);
            myCommand.CommandType			= CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabDB DeleteTab  itemID: {0}, ex: {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

        /// <summary>
        /// The AddTab method adds a new announcement to the
        /// HtmlTabs database table, and returns the ItemID value as a result.
        /// </summary>
        public int AddTab(int moduleID,string title,string description,bool IsBreak,int TabOrder) 
        {
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_AddHtmlTab", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            SqlParameter parameterItemID		=	new SqlParameter(StStr.ATItemID, SqlDbType.Int, 4);
            parameterItemID.Direction           =   ParameterDirection.Output;
            myCommand.Parameters.Add(parameterItemID);

            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterTabOrder      =   new SqlParameter("@TabOrder", SqlDbType.Int);
            parameterTabOrder.Value             =   TabOrder;
            myCommand.Parameters.Add(parameterTabOrder);

            SqlParameter parameterTitle         =   new SqlParameter("@Title", SqlDbType.NVarChar, 150);
            parameterTitle.Value                =   title;
            myCommand.Parameters.Add(parameterTitle);

            SqlParameter parameterIsBreak      =   new SqlParameter("@IsBreak", SqlDbType.Bit);
            parameterIsBreak.Value             =   IsBreak;
            myCommand.Parameters.Add(parameterIsBreak);

            SqlParameter parameterDescription   =   new SqlParameter("@Description", SqlDbType.NVarChar, description.Length);
            parameterDescription.Value          =   description;
            myCommand.Parameters.Add(parameterDescription);

			int ret							    =	-1;
			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
				ret			                    =	(int)parameterItemID.Value;
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabDB AddTab  moduleID: {0}, ex: {0}",moduleID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
			return ret;
        }

        public void UpdateTab(int moduleID, int itemID,string title,string description,bool IsBreak,int TabOrder) 
        {
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_UpdateHtmlTab", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

            SqlParameter parameterTabOrder      =   new SqlParameter("@TabOrder", SqlDbType.Int);
            parameterTabOrder.Value             =   TabOrder;
            myCommand.Parameters.Add(parameterTabOrder);

            SqlParameter parameterTitle         =   new SqlParameter("@Title", SqlDbType.NVarChar, 150);
            parameterTitle.Value                =   title;
            myCommand.Parameters.Add(parameterTitle);

            SqlParameter parameterIsBreak      =   new SqlParameter("@IsBreak", SqlDbType.Bit);
            parameterIsBreak.Value             =   IsBreak;
            myCommand.Parameters.Add(parameterIsBreak);

            SqlParameter parameterDescription   =   new SqlParameter("@Description", SqlDbType.NVarChar, description.Length);
            parameterDescription.Value          =   description;
            myCommand.Parameters.Add(parameterDescription);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabDB UpdateTab itemID: {0}, ex : {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

		public DataTable GetTabsGlobalized(int moduleID) 
		{
			SqlDataAdapter myAdapt			    =   new SqlDataAdapter("dbo.adn_GetHtmlTabsGlobalized", myConnection);
			myAdapt.SelectCommand.CommandType   =   CommandType.StoredProcedure;

			AddGlobalizeParameters(myAdapt.SelectCommand);

            AddModuleIDParameters(myAdapt.SelectCommand,moduleID);

			DataTable myDataSet					=   new DataTable();
			try
			{
				myAdapt.Fill(myDataSet);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabDB GetTabsGlobalized ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
			return CheckDataTableForDates(myDataSet);
		}

        /// <summary>
		/// The GetSingleTab method returns a SqlDataReader containing details
		/// about a specific tab from the HtmlTabs database table.
		/// </summary>
		public SqlDataReader GetSingleTabGlobalized(int itemID) 
		{
			SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetSingleHtmlTabGlobalized", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

			AddGlobalizeParameters(myCommand);

            AddItemIDParameters(myCommand,itemID);


			SqlDataReader result			    =   null;
			try
			{
				myConnection.Open();
				result						    =   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]HtmlTabDB GetSingleTabGlobalized itemID: {0}, ex: {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}
	
		public void DeleteTabGlobalized(int itemID) 
		{
			SqlCommand myCommand			    =   new SqlCommand("dbo.adn_DeleteHtmlTabGlobalized", myConnection);
			myCommand.CommandType			    =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);


			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]AnnouncementzsDB DeleteTabGlobalized  itemID: {0}, ex: {1}",itemID, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

		public void UpdateTabGlobalized(int moduleID, int itemID,string title,string description,bool IsBreak,int TabOrder) 
		{

			SqlCommand myCommand				=   new SqlCommand("dbo.adn_UpdateHtmlTabGlobalized", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

			AddGlobalizeParameters(myCommand);

            AddItemIDParameters(myCommand,itemID);

            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterTabOrder      =   new SqlParameter("@TabOrder", SqlDbType.Int);
            parameterTabOrder.Value             =   TabOrder;
            myCommand.Parameters.Add(parameterTabOrder);

			SqlParameter parameterTitle         =   new SqlParameter("@Title", SqlDbType.NVarChar, 150);
			parameterTitle.Value                =   title;
			myCommand.Parameters.Add(parameterTitle);

			SqlParameter parameterIsBreak      =   new SqlParameter("@IsBreak", SqlDbType.Bit);
			parameterIsBreak.Value             =   IsBreak;
			myCommand.Parameters.Add(parameterIsBreak);

			SqlParameter parameterDescription   =   new SqlParameter("@Description", SqlDbType.NVarChar, description.Length);
			parameterDescription.Value          =   description;
			myCommand.Parameters.Add(parameterDescription);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]AnnouncementzsDB UpdateTabGlobalized  itemID: {0}, ex: {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

        /// <summary>
		/// Only updates the Tab Order
		/// </summary>
        public void UpdateTabOrderGlobalized(int moduleID, int itemID,int TabOrder) 
		{

			SqlCommand myCommand				=   new SqlCommand("dbo.adn_UpdateHtmlTabOrderGlobalized", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

			AddGlobalizeParameters(myCommand);

            AddItemIDParameters(myCommand,itemID);

            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterTabOrder      =   new SqlParameter("@TabOrder", SqlDbType.Int);
            parameterTabOrder.Value             =   TabOrder;
            myCommand.Parameters.Add(parameterTabOrder);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]AnnouncementzsDB UpdateTabOrderGlobalized  itemID: {0}, ex: {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}
	}
}
